package com.frxuexi.park2209_2

import android.content.Intent
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.base.bean.HomeBean
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209_2.adapter.CityMenuAdapter
import com.frxuexi.park2209_2.adapter.HourseAdapter
import com.frxuexi.park2209_2.adapter.MenusAdapter
import com.frxuexi.park2209_2.databinding.ActivityMainBinding
import com.frxuexi.park2209_2.hourse.HourseDetailActivity
import com.frxuexi.park2209_2.hourse.HourseViewModel
import com.frxuexi.park2209_2.hourse.UserProActivity
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, HourseViewModel>() {
    lateinit var cityMenuAdapter: CityMenuAdapter
    lateinit var MenusAdapter: MenusAdapter
    val hourseAdapter by lazy { HourseAdapter() }

    override fun initData() {
        vm.homeSuc.observe(this, object : Observer<HomeBean> {
            override fun onChanged(value: HomeBean) {
                Log.i("===", value.toString())
                cityMenuAdapter.addData(value.cityMenuMap.baseMenus)
                cityMenuAdapter.notifyDataSetChanged()

                MenusAdapter.addData(value.cityMenuMap.menus)
                MenusAdapter.notifyDataSetChanged()

                hourseAdapter.addData(value.firstPageRoomList)
                hourseAdapter.notifyDataSetChanged()
            }

        })
    }

    override fun initView() {
        cityMenuAdapter = CityMenuAdapter()
        MenusAdapter = MenusAdapter()
        binding.rvCitymenu.apply {
            adapter = cityMenuAdapter
            layoutManager = GridLayoutManager(this@MainActivity, 5)
        }

        binding.rvMenus.apply {
            adapter = MenusAdapter
            layoutManager = GridLayoutManager(this@MainActivity, 5)
        }

        binding.rvHours.apply {
            adapter = hourseAdapter
            layoutManager = LinearLayoutManager(this@MainActivity)
        }
        hourseAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@MainActivity, HourseDetailActivity::class.java))
        }
        vm.getHomeMsg()
//        val isYes = SPUtils.getInstance().getBoolean("isyes", false)
//        if(isYes)
//            return
        val dialog = LayoutInflater.from(this).inflate(R.layout.dialog_view, null)
        val alertDialog = AlertDialog.Builder(this).setView(dialog)
            .create()
        dialog.findViewById<TextView>(R.id.btn_yes)
            .setOnClickListener {
                alertDialog.dismiss()
                SPUtils.getInstance().put("isyes",true)
            }

        var str = "选择同意就表示充分阅读，理解并接受《用户协议》与《隐私协议》"
        val ss = SpannableString(str)
        val start = str.indexOf("《用户协议》")

        val start2 = str.indexOf("《隐私协议》")

        // 4. 设置部分文本为可点击
        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                alertDialog.dismiss()
                val intent = Intent(this@MainActivity, UserProActivity::class.java)
                intent.putExtra("url","https://m.qfang.com/shenzhen/user/user-protocol.html")
                startActivity(intent)
//                ARouter.getInstance().build("/app/UserProActivity").navigation()

            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.BLUE // 设置点击文本的颜色
                ds.isUnderlineText = false // 是否显示下划线
            }
        }
        ss.setSpan(
            clickableSpan,
            start, start + 6,  // 范围：从第17个字符到第23个字符
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        val clickableSpan2: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                alertDialog.dismiss()
                val intent = Intent(this@MainActivity, UserProActivity::class.java)
                intent.putExtra("url","https://m.qfang.com/shenzhen/user/privacy-protocol.html")
                startActivity(intent)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.BLUE // 设置点击文本的颜色
                ds.isUnderlineText = false // 是否显示下划线
            }
        }
        ss.setSpan(
            clickableSpan2,
            start2, start2 + 6,  // 范围：从第17个字符到第23个字符
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        val tv_content = dialog.findViewById<TextView>(R.id.dialog_content)
        tv_content.text = ss
        tv_content.setMovementMethod(LinkMovementMethod.getInstance());
       alertDialog.show()



    }

}